const db = require("../dbUtil/index");
const sql = require("../dbUtil/sql");
const functionCtl = require("./function/function");

module.exports = {
    //新歌
    newSongs: (req, res) => {
        let newSongID = 1;
        var newSongs = [];
        functionCtl.manySongs(newSongID, newSongs, sql.listen.newSong, sql.listen.newSong2, res);
        return;
    },
    //热歌
    recommends: (req, res) => {
        let recommendID = 1;
        var recommends = [];
        functionCtl.manySongs(recommendID, recommends, sql.listen.recommend, sql.listen.recommend2, res);
        return;
    },
    //内陆,港澳，欧美，韩国，日本
    regions: (req, res) => {
        let { regionID } = req.query;
        var regions = [];
        try {
            functionCtl.manySongs(regionID, regions, sql.listen.region, sql.listen.region2, res);
        } catch (error) {
            console.log("");
        }
        return;
    },
    //宣泄，寂寞，甜蜜，思念，治愈，励志，安静，快乐，伤感
    calssifys: (req, res) => {
        let { calssifyID } = req.query;
        var calssifys = [];
        functionCtl.manySongs(calssifyID, calssifys, sql.listen.calssify, sql.listen.calssify2, res);
        return;
    },
    //点歌曲
    dotSong: (req, res) => {
        var dotSong = [];
        var { songsID } = req.query;
        functionCtl.oneSong(songsID, dotSong, sql.listen.dotSong, res);
    },
    //点歌手
    dotSinger: (req, res) => {
        var dotSinger = [];
        var { singerID } = req.query;
        functionCtl.oneSong(singerID, dotSinger, sql.listen.dotSinger, res);
    },
    //搜歌曲/歌手
    searchSong: (req, res) => {
        var searchSong = [];
        var { songsName } = req.query;
        var sql2, sql3, sql4, sql5;
        sql2 = sql.listen.searchSong.split("?");
        sql3 = sql2[0] + songsName + sql2[1];
        db.query(sql3, [], (err, results) => {
            if (err) {
                //返回一个json对象：状态码、状态描述、数据
                return res.send({
                    code: 505,
                    msg: "数据库执行错误！",
                    data: {}
                });
            } else {
                if (results.length > 0) {
                    for (var i in results) {
                        function c() {
                            return new Promise((resolve, reject) => {
                                db.query(sql.listen.searchSong2, [results[i].songName], (err2, results2) => {
                                    if (err2) {
                                        reject(err2);
                                    } else {
                                        resolve(results2);
                                    }
                                })
                            })
                        }
                        c().then((results2) => {
                            var singerName = [];
                            var singerID = [];
                            for (var j in results2) {
                                singerName.push(results2[j].singerName);
                                singerID.push(results2[j].singerID);
                            }
                            results2[0].singerName = singerName;
                            results2[0].singerID = singerID;
                            //时长
                            var arr1 = results2[0].songWord.split("[");
                            var arr2 = arr1[arr1.length - 1].split("]");
                            var time = arr2[0].substring(1, 5);
                            results2[0].time = time;
                            searchSong.push(results2[0]);
                            if (searchSong.length == results.length) {
                                //获取数据库数据
                                res.send({
                                    code: 200,
                                    msg: "数据库执行成功！",
                                    data: { data: searchSong }
                                });
                            }
                        }).catch((err) => {
                            //返回一个json对象：状态码、状态描述、数据
                            return res.send({
                                code: 505,
                                msg: "数据库2执行错误！",
                                data: { err }
                            });
                        })
                    }
                } else {
                    sql4 = sql.listen.searchSinger.split("?");
                    sql5 = sql4[0] + songsName + sql4[1];
                    db.query(sql5, [], (err3, results3) => {
                        if (err3) {
                            //返回一个json对象：状态码、状态描述、数据
                            return res.send({
                                code: 505,
                                msg: "数据库执行错误！",
                                data: {}
                            });
                        } else {
                            for (var i in results3) {
                                function c() {
                                    return new Promise((resolve, reject) => {
                                        db.query(sql.listen.searchSinger2, [results3[i].singerName], (err4, results4) => {
                                            if (err4) {
                                                reject(err4);
                                            } else {
                                                resolve(results4);
                                            }
                                        })
                                    })
                                }
                                c().then((results4) => {
                                    //时长
                                    var arr1 = results4[0].songWord.split("[");
                                    var arr2 = arr1[arr1.length - 1].split("]");
                                    var time = arr2[0].substring(1, 5);
                                    results4[0].time = time;
                                    searchSong.push(results4[0]);
                                    if (searchSong.length == results3.length) {
                                        //获取数据库数据
                                        res.send({
                                            code: 200,
                                            msg: "数据库执行成功！",
                                            data: { data: searchSong }
                                        });
                                    }
                                }).catch((err) => {
                                    //返回一个json对象：状态码、状态描述、数据
                                    return res.send({
                                        code: 505,
                                        msg: "数据库2执行错误！",
                                        data: { err }
                                    });
                                })
                            }
                        }
                    })
                }
            }
        })
    },
    //列表歌单
    songLists: (req, res) => {
        db.query(sql.listen.songLists, [], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作错误！",
                    data: {}
                })
            } else {
                var songLists = [];
                for (var i in results) {
                    db.query(sql.listen.songLists2, [results[i].songListName], (err2, results2) => {
                        if (err2) {
                            res.send({
                                code: 505,
                                msg: "数据库操作错误！",
                                data: {}
                            })
                        } else {
                            songLists.push(results2[0]);
                            if (songLists.length == results.length) {
                                res.send({
                                    code: 200,
                                    msg: "数据库操作成功！",
                                    data: { data: songLists }
                                })
                            }
                        }
                    })
                }
            }
        })
    },
    //点歌单
    dotSongList: (req, res) => {
        let { songListName, userID } = req.query;
        db.query(sql.listen.songLists3, [songListName], (err, results) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "数据库操作失败！",
                    data: {}
                })
            } else {
                var songList = [];
                var like = 0;
                db.query(sql.listen.songLists5, [userID, songListName], (err, results3) => {
                    if (err) {
                        return res.send({
                            code: 505,
                            msg: "数据库2执行错误！",
                            data: {}
                        });
                    } else {
                        if (results3.length > 0) {//是否收藏
                            like += 1;
                            functionCtl.manysongsID(songList, results, sql.listen.songLists4, res, like);
                        } else {
                            like = 0;
                            functionCtl.manysongsID(songList, results, sql.listen.songLists4, res, like);
                        }
                    }
                })
            }
        })
    },
    //logo
    logo: (req, res) => {
        db.query("select singerImg from singers where singerName = 'logo';", [], (err, img) => {
            if (err) {
                res.send({
                    code: 505,
                    msg: "请求数据库错误！",
                    data: {},
                })
            } else {
                res.send({
                    code: 200,
                    msg: "请求数据库成功！",
                    data: { img },
                })
            }
        })
    }
}